import { defineStore } from 'pinia'
import Cookies from 'js-cookie'

export const useAppStore = defineStore('app', {
  // convert to a function
  state: () => ({
    sidebar: {
      opened: !+Cookies.get('sidebarStatus'),
      withoutAnimation: false
    },
    device: 'desktop'
  }),
  // 相当于组件中的methods，适合用于定义组件的业务逻辑。
  actions: {
    toggleSideBar () {
      if (this.sidebar.opened) {
        Cookies.set('sidebarStatus', 1)
      } else {
        Cookies.set('sidebarStatus', 0)
      }
      this.sidebar.opened = !this.sidebar.opened
      this.sidebar.withoutAnimation = false
    },
    closeSideBar (withoutAnimation) {
      Cookies.set('sidebarStatus', 1)
      this.sidebar.opened = false
      this.sidebar.withoutAnimation = withoutAnimation
    },
    openSideBar () {
      if (!this.sidebar.opened) {
        Cookies.set('sidebarStatus', 0)
        this.sidebar.opened = true
      }
    },
    toggleDevice (device) {
      this.device = device
    }
  }
})